﻿//GENERAL GRID METHODS

//public methods
var resizeGrid;
var resizeSubGrid;
var showColumnChooser;
var setPageFilter;
var PageFilter;
var ShowGrid;
var ShowDetail;
var CreateGrid;
var haskey = false;

//public variables
var gridconfigtype;
var from_outside = false;

//doc ready
$(document).ready(function () {

    //PRIVATE VARIABLES
    var canvaswidth = $('#site_top_middle').width();
    var canvasheight = $('#site_top_middle').height();
    var loadingPanel = $('#loaderwrapper');
    var loader = $('#loader');
    var glasswrapper = $('#glasswrapper');
    var glass = $('#DialogGlass');
    var grid = jQuery("#list");
    var containerwidth = 876;
    haskey = false;
    $.guid = 0;

    //*************
    //INIT
    //*************
    loader.css({ height: canvasheight - 182, width: canvaswidth });
    loadingPanel.show();
    glasswrapper.hide();

    //*************
    //METHODS
    //*************

    resizeGrid = function () {
        //manages the resizing of the grid if the browser is resized
        try {
            $('.widegrid').jqGrid('setGridWidth', 876);

            var twidth = 0;
            containerwidth = 876;
            $('.container-content').each(function (index) {
                twidth = $(this).width();
                if (twidth > containerwidth) {
                    containerwidth = twidth;
                }
            });
            $('.widegrid').jqGrid('setGridWidth', containerwidth - 2);
            //loader.css({ height: canvasheight - 182, width: containerwidth+60 });
            canvaswidth = $('#site_top_middle').width();
            canvasheight = $('#site_top_middle').height();
            containerwidth = $('.container-content').width();
        }
        catch (err) {
        }
        finally {
            loader.css({ height: canvasheight - 182, width: canvaswidth });
            $('#loaderwrapper').hide();
            var glass = $('#DialogGlass');
            glass.css({ height: $('#site').height(), width: $('#site').width() });
        }
    }

    resizeFullSubGrid = function (list, container) {
        try {
            canvasheight = $('#site_top_middle').height();

            loader.css({ height: canvasheight - 182, width: canvaswidth });
            $(list).jqGrid('setGridWidth', 920);
            var containerdiv = $(container);
            var containerwidth = $(containerdiv).width();
            $(list).jqGrid('setGridWidth', containerwidth - 2);

            //$('#loaderwrapper').hide();
            glass.css({ height: $('#site').height(), width: $('#site').width() });
        }
        catch (err) { }
    }

    resizeSubGrid = function (list, container) {
        try {
            canvasheight = $('#site_top_middle').height();

            loader.css({ height: canvasheight - 182, width: canvaswidth });
            $(list).jqGrid('setGridWidth', 920);
            var containerwidth = $(container).width();
            if (containerwidth > 500) {
                $(list).jqGrid('setGridWidth', 920 - 2);
            }
            else {
                $(container).width(920);
            }

            //$('#loaderwrapper').hide();
            glass.css({ height: $('#site').height(), width: $('#site').width() });
        }
        catch (err) { }
    }

    showColumnChooser = function () {

        glass.css({ height: $('#site').height(), width: $('#site').width() });
        glasswrapper.show();
        $("#list").jqGrid('columnChooser', { width: 600, done: function (perm) {
            if (perm) {
                $("#list").jqGrid("remapColumns", perm, true);
                SetVCConfig();
                var pg = $("#list").getGridParam('page');
                overridepage = pg;
                GetVCConfig();
                $("#list").jqGrid('GridUnload');
                $("#list").jqGrid('GridDestroy');
                CreateGrid();
                $("#list").trigger("reloadGrid");
                overridepage = 0;

            }

            glasswrapper.hide();
        }
        });
    }

    //subtype is optional
    exportGrid = function (subtype) {
        var hidsearchid = $('#hidsearchid').val();

        var pagehref = '/Pages/Viewer/GridExporter.aspx?seid=' + hidsearchid + '&subtype=' + subtype;
        window.open(pagehref, '_blank');
    }

    StopCheck = function () {
        grid.jqGrid('resetSelection');
    }

    PageFilter = function() {
        var hidsearchid = $('#hidsearchid').val();
        //var hidsearchid = $('#txtSid').val();
        if (hidsearchid.length <= 0) {

            var time = new Date();
            time = time.getTime();
            $.guid = time.toString();
            hidsearchid = $.guid;
            $('#hidsearchid').val(hidsearchid);
        }
        var pathname = window.location.pathname;
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Service/Services.aspx/PageFilter",
            data: '{seid:"' + hidsearchid + '", url:"' + pathname + '"}',
            dataType: "json",
            success: function(data, textStatus) {
                var detail = false;
                $('#loaderwrapper').show();
                if (textStatus == "success") {
                    var msg = jQuery.parseJSON(data.d);

                    if (msg.Message != null) {

                        var filter = jQuery.parseJSON(msg.Message);
                        if (filter != null) {
                            var nparams;
                            if ((filter.Params != null) && (filter.Params.length > 0)) {
                                nparams = { andor: '&&', items: [] };
                                var sparms = filter.Params.split(':');
                                nparams.andor = sparms[0];
                                var sitems = sparms[1].split(';');
                                for (var i = 0; i < sitems.length; i++) {
                                    var item = sitems[i].split(',');
                                    var pitem = {
                                        column: item[0],
                                        datatype: item[1],
                                        operator:
                                            item[2],
                                        value0: item[3],
                                        value1: item[4],
                                        value2: item[5],
                                        value3: item[6]
                                    };
                                    nparams.items.push(pitem)
                                }
                                $("#advancedsearch").show();
                                $('#as_openlink a').html('advanced search <img src="/images/icon-collapse.gif" class="as_link_icon" /></a>');
                                //CreateGrid();
                            }
                            SetSearchParams(nparams);
                            if ((filter.Key != null) && (filter.Key > 0)) {
                                haskey = true;
                            }
                            CreateGrid();
                            if ((filter.Key != null) && (filter.Key > 0)) {
                                filtered = true;
                                GetSelectedItem(filter.Key, filter.Internal);
                                detail = true;
                            }
                        } else {
                            SetSearchParams();
                            CreateGrid();
                        }
                    } else {
                        CreateGrid();
                    }
                }
                if (!detail) {
                    ShowGrid();
                } else {

                }
            },
            error: function(data, textStatus) {
                CreateGrid();
                ShowGrid();
            }
        });
    };

    ShowGrid = function () {
        $('#panel_grid_main').show();
        $('#panel_grid_sub').hide();
        $('#panel_tabs').hide();
        resizeGrid();
        $('#loaderwrapper').show();

    }

    ShowDetail = function () {
        $('#panel_grid_main').hide();

        $('#panel_grid_sub').show();
        $('#panel_tabs').show();


        $('.subnav_ref').removeClass('navon');
        $('.subnav_ref').first().addClass('navon');
        $('.switchpanel').hide();
        var selpanel = $('.subnav_ref').first().attr("href");
        $(selpanel).show();
        canvasheight = $('#site_top_middle').height();
        loader.css({ height: canvasheight - 182, width: canvaswidth });
        preSave();
        try {
            var containerwidth = $('#second_grid_container').width();
            $('#second_list').jqGrid('setGridWidth', containerwidth - 2);
        }
        catch (err) {
        }
    }

    ShowDetailNoNav = function () {
        $('#panel_grid_main').hide();
        $('#panel_grid_sub').show();
        $('#panel_tabs').show();
        canvasheight = $('#site_top_middle').height();
        loader.css({ height: canvasheight - 182, width: canvaswidth });
//        preSave();
        try {
            var containerwidth = $('#second_grid_container').width();
            $('#second_list').jqGrid('setGridWidth', containerwidth - 2);
        }
        catch (err) {

        }
    }

    reloadData = function (sessiontype) {
        resizeGrid();
        $('#loaderwrapper').show();
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Service/Services.aspx/clearSession",
            data: '{sessiontype:"' + sessiontype + '"}',
            dataType: "json",
            success: function (data, textStatus) {
                if (textStatus == "success") {
                    $("#list").trigger("reloadGrid");

                }
                resizeGrid();
            },
            error: function (data, textStatus) {
                ShowGrid();
                $("#list").trigger("reloadGrid");
                resizeGrid();
            }
        });
    }

    TriggerAdvSearchPanel = function () {
        if ($("#advancedsearch").is(":hidden")) {
            $("#advancedsearch").slideDown("slow");
            $(this).html('advanced search <img src="/images/icon-collapse.gif" class="as_link_icon" /></a>');
        } else {
            $("#advancedsearch").slideUp("slow");
            $(this).html('advanced search <img src="/images/icon-expand.gif" class="as_link_icon" /></a>');
        }
        return false;
    }

    MultiRecAction = function (paramstring) {
        var msg = { ReturnCode: -1, Message: "" };
        $.ajax({
            type: "POST",
            async: false,
            contentType: "application/json; charset=utf-8",
            url: "/Service/Services.aspx/MultiRecAction",
            data: paramstring,
            dataType: "json",
            success: function (data, textStatus) {
                if (textStatus == "success") {
                    msg = jQuery.parseJSON(data.d);
                }
                $("#list").trigger("reloadGrid");
            },
            error: function (data, textStatus) {
                msg = { ReturnCode: -1, Message: "A communication error as occurred.  Please try again." };
            }
        });
        return msg;
    }

    CancelClick = function () {
        var hidsearchid = $('#hidsearchid').val();
        var paramstring = '{seid:"' + hidsearchid + '"}';
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Service/Services.aspx/PriorRecent",
            data: paramstring,
            dataType: "json",
            success: function (data, textStatus) {
                if (textStatus == "success") {
                    var msg = jQuery.parseJSON(data.d);
                    if (msg.ReturnCode == 1) {
                        if (msg.Message != null) {
                            window.location = msg.Message;
                        }
                    }
                    else {
                        ShowGrid();
                        $("#list").trigger("reloadGrid");
                        $('#loaderwrapper').hide();
                    }
                }
            },
            error: function (data, textStatus) {
                ShowGrid();
                $("#list").trigger("reloadGrid");
                $('#loaderwrapper').hide();
            }
        });
    }
    //@end methods


    //*************
    //EVENTS
    //*************
    $('#as_openlink a').click(function () {
        if ($("#advancedsearch").is(":hidden")) {
            $("#advancedsearch").slideDown("slow");
            $(this).html('advanced search <img src="/images/icon-collapse.gif" class="as_link_icon" /></a>');
        } else {
            $("#advancedsearch").slideUp("slow");
            $(this).html('advanced search <img src="/images/icon-expand.gif" class="as_link_icon" /></a>');
        }
        $(this).blur();
        return false;
    });

    $('#multi_action_list').change(function () {
        var vlu = $('#multi_action_list option:selected').val();
        if (vlu.length > 2) {
            $('#btn_multiaction_go').button("option", "disabled", false);
        }
        else {
            $('#btn_multiaction_go').button("option", "disabled", true);
        }
    });
    //@end events



    $("#advancedsearch").hide();

    $.jgrid.formatter.integer.thousandsSeparator = ',';
    $.jgrid.formatter.number.thousandsSeparator = ',';
    $.jgrid.formatter.currency.thousandsSeparator = ',';

});



$(window).resize(function () {
    try {
        resizeGrid();
        resizeSubGrid();
    }
    catch (err) { }
});